import 'dart:convert';
import 'dart:math';

import 'package:crypto/crypto.dart';
import 'package:date_format/date_format.dart';
import 'package:intl/intl.dart';
import 'package:kq_flutter_core_widget/utils/ex/kq_ex.dart';
import 'package:uuid/uuid.dart';

/// 字符串辅助类
class StrUtil {
  /// 判断字符串、集合、字典是否为空
  static bool isEmpty(dynamic obj) {
    if (obj is String) {
      return obj.isEmpty;
    }
    if (obj is Map) {
      return obj.isEmpty;
    }
    if (obj is Iterable) {
      return obj.isEmpty;
    }
    return obj == null;
  }

  /// 生成uuid
  static String uuid() {
    var uuid = const Uuid();
    return uuid.v4();
  }

  /// md5加密
  static String md5Crypto(String str) {
    var bytes = utf8.encode(str);
    final dig = md5.convert(bytes);
    var keyStr = dig.toString();
    return keyStr;
  }

  /// 获取指定长度的随机字母字符串
  static String getRandomStr(int length) {
    String alphabet = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    String left = "";
    for (var i = 0; i < length; i++) {
      left = left + alphabet[Random().nextInt(alphabet.length)];
    }
    return left;
  }

  /// 获取GMT格式时间
  static String getGMTDateString() {
    var date = DateTime.now();
    date = date.subtract(const Duration(hours: 8));
    return DateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", 'en').format(date);
  }

  /// 格式化日期 yyyy-mm
  static String formatDatetimeYM(DateTime dateTime, {String split = '-'}) {
    return formatDate(dateTime, [yyyy, split, mm]);
  }

  /// 格式化日期 yyyy-mm-dd
  static String formatDatetimeYMD(DateTime dateTime, {String split = '-'}) {
    return formatDate(dateTime, [yyyy, split, mm, split, dd]);
  }

  /// 格式化日期和小时 yyyy-MM-dd HH
  static String formatDatetimeYMDH(
    DateTime dateTime, {
    String dateSplit = '-',
    String hourUnit = '',
    bool widthSpace = true,
  }) {
    return formatDate(dateTime, [
      yyyy,
      dateSplit,
      mm,
      dateSplit,
      dd,
      widthSpace ? ' ' : '',
      HH,
      hourUnit
    ]);
  }

  /// 格式化时间 HH:mm
  static String formatDatetimeHm(DateTime dateTime, {String split = ':'}) {
    return formatDate(dateTime, [HH, split, nn]);
  }

  /// 格式化时间 HH:mm:ss
  static String formatDatetimeHms(DateTime dateTime, {String split = ':'}) {
    return formatDate(dateTime, [HH, split, nn, split, ss]);
  }

  /// 格式化日期和时间 yyyy-MM-dd HH:mm
  static String formatDatetimeYMDHm(DateTime dateTime, {String split = '-'}) {
    return formatDate(dateTime, [yyyy, split, mm, split, dd, ' ', HH, ':', nn]);
  }

  /// 格式化日期和时间 yyyy-MM-dd HH:mm:ss
  static String formatDatetimeYMDHms(DateTime dateTime,
      {String dateSplit = '-', String timeSplit = ':'}) {
    return formatDate(dateTime, [
      yyyy,
      dateSplit,
      mm,
      dateSplit,
      dd,
      ' ',
      HH,
      timeSplit,
      nn,
      timeSplit,
      ss
    ]);
  }

  // 获取当前月份天数
  static int getCurrentMonthDays({required int year, required int month}) {
    if (month == 2) {
      if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
        return 29;
      } else {
        return 28;
      }
    } else if (month == 1 ||
        month == 3 ||
        month == 5 ||
        month == 7 ||
        month == 8 ||
        month == 10 ||
        month == 12) {
      return 31;
    } else {
      return 30;
    }
  }

  /// 动态拼接数据
  /// [concatWhenEmpty] 为空时是否拼接
  static String? concatDynamic<T>(
      List<T>? list, String? Function(T t) concatConverter,
      {String split = ',',
      String? defaultValue,
      bool concatWhenEmpty = false}) {
    if (list == null) {
      return defaultValue;
    }
    String str = '';
    for (var value in list) {
      if (str.isEmpty) {
        str = concatConverter(value) ?? '';
      } else {
        str = concatString(str, concatConverter(value), split,
            concatWhenEmpty: concatWhenEmpty);
      }
    }
    return str.isEmpty ? defaultValue : str;
  }

  /// 拼接字符串，中间使用[split]分割，如果有一个字符串为空，则不显示[split]
  /// [concatWhenEmpty] 为空时是否拼接
  static String concatString(String? str1, String? str2, String split,
      {bool concatWhenEmpty = false}) {
    if (str1.isNullOrEmpty) {
      if (concatWhenEmpty) {
        return '$split${str2 ?? ''}';
      } else {
        return str2 ?? '';
      }
    }
    if (str2.isNullOrEmpty) {
      if (concatWhenEmpty) {
        return '$str1$split';
      } else {
        return str1!;
      }
    }
    return '$str1$split$str2';
  }

  /// 获取列表数据中的单个指定key列表
  static List<V> concatValueToList<V, T>(
      List<T>? list, V Function(T t) concatConverter) {
    List<V> rList = [];
    if (list == null) {
      return rList;
    }
    for (T t in list) {
      rList.add(concatConverter(t));
    }
    return rList;
  }

  ///类型判断
  static T? getValue<T>(var value) {
    if (value == null) {
      return null;
    } else if (T == bool) {
      return (value == "1" || value == "true" || value is bool) as T;
    } else if (T == String) {
      return value as T;
    } else if (T == int) {
      return int.parse(value) as T;
    } else if (T == double) {
      return double.parse(value) as T;
    } else {
      return value;
    }
  }
}
